package com.lyn.plugin.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * so加载工具类
 * @author longyn
 * @version 1.0.0
 */
public class SoUtil {

    /**
     * 获取插件So
     * @param path 插件路径
     */
    public static void extractPlugInSoFile(String path,String pluginSoPath){
        InputStream inputStream=null;
        FileOutputStream outputStream=null;
        String cpuAbi=getCPUAbi();
        try {
            ZipFile zipFile = new ZipFile(path);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry zipEntry = entries.nextElement();
                String zipEntryName = zipEntry.getName();
                if (!zipEntryName.startsWith("lib")) {
                    continue;
                }
                if (!zipEntryName.contains(cpuAbi)) {
                    continue;
                }
                if (!zipEntryName.endsWith(".so")) {
                    continue;
                }
                inputStream = zipFile.getInputStream(zipEntry);
                String[] split = zipEntryName.split("/");
                String soFileName = split[split.length - 1];
                File soFile = new File(pluginSoPath,soFileName);
                if (!soFile.exists()) {
                    soFile.createNewFile();
                }
                outputStream = new FileOutputStream(soFile);
                byte[] data = new byte[1024];
                int length;
                while ((length = inputStream.read(data)) != -1) {
                    outputStream.write(data, 0, length);
                }
                inputStream.close();
                outputStream.flush();
                outputStream.close();
            }
        }catch (Throwable e){
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            }catch (Exception ignored){
            }
            try {
                outputStream.flush();
                outputStream.close();
            }catch (Exception ignored){
            }
        }
    }


    /**
     * 获取cpu架构
     * @return cpu架构
     */
    public static String getCPUAbi(){
        return execShell("getprop ro.product.cpu.abi");
    }

    /**
     * 执行adb命令
     * @param cmd 命令
     * @return 结果
     */
    public static String execShell(String cmd) {
        StringBuilder sb = new StringBuilder();
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(cmd);
            InputStream inputStream = process.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            process.waitFor();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            inputStream.close();
            bufferedReader.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
